<template>
  <div class="recommend">
    <scroll ref="scroll" class="recommend-content" :data="discListData">
      <div>
        <div v-if="recommendList.length" class="slider-wrapper" ref="sliderWrapper">
        <slider>
          <div v-for="(item,index) in recommendList" :key=index>
            <a :href="item.linkUrl">
              <img class="needsclick" @load="loadImage" :src="item.picUrl" alt="">
            </a>
          </div>
        </slider>
      </div>
      <div class="recommend-list">
        <h1 class="list-title">热门歌单推荐</h1>
        <ul>
          <li class="item" v-for="(item,index) in discListData" :key="index" @click="clickGoDesc(item)">
            <div class="icon">
              <img width="60" height="60" v-lazy="item.imgurl" alt="">
            </div>
            <div class="text">
              <h2 class="name" v-html="item.creator.name"></h2>
              <p class="desc" v-html="item.dissname"></p>
            </div>
          </li>
        </ul>
      </div>
      </div>
      <div class="loading-container" v-show="!discListData.length">
        <loading></loading>
      </div>
    </scroll>
    <router-view></router-view>
  </div>
</template>

<script>
import { getRecommendData, getDiscList } from '@/api/recommend'
import { ERR_OK } from '@/api/config'
import slider from '@/base/slider/slider'
import scroll from '@/base/scroll/scroll'
import loading from '@/base/loading/loading'
import {mapMutations} from 'vuex'

export default {
  components: {
    slider,
    scroll,
    loading
  },
  props: {},
  data() {
    return {
      recommendList: [],
      discListData: []
    }
  },
  watch: {},
  computed: {},
  methods: {
    ...mapMutations({
      setDesc: 'SET_DESC'
    }),
    clickGoDesc(item) {
      this.$router.push({
        path: `/recommend/${item.dissid}`
      })
      this.setDesc(item)
    },
    handleRecommData() {
      getRecommendData().then((res) => {
        if (res.code === ERR_OK) {
          this.recommendList = res.data.slider
        }
      })
    },
    handlePlayList() {
      getDiscList().then((res) => {
        this.discListData = res.data.list
      })
    },
    loadImage() {
      if (!this.checkLoading) {
        //  坑 监听图片加载 之后会有一个样式赋予的延迟 导致计算高度出错
        // 所以要加一个 setimeout的延迟
        this.checkLoading = true
        setTimeout(() => {
          this.$refs.scroll.refresh()
        }, 20)
      }
    }
  },
  created() {
    this.handleRecommData()
    // setTimeout(() => {
    this.handlePlayList()
    // }, 1000)
  },
  mounted() {
    if (this.scroll) {
      this.scroll.on('scroll', () => {
        console.log('scroll--------')
      })
    }
    // this.$nextTick(() => {
    //   console.log(this.$refs.sliderWrapper.className)
    // })
  }
}
</script>
<style lang="stylus" rel="stylesheet/stylus" scoped>
@import '~common/stylus/variable.styl'
.recommend
  position: fixed
  width: 100%
  bottom: 0
  top: 88px
  .recommend-content
    height: 100%
    overflow: hidden
    .slider-wrapper
      position: relative
      width: 100%
      overflow: hidden
    .recommend-list
      .list-title
        height: 65px
        line-height: 65px
        text-align: center
        font-size: $font-size-medium
        color: $color-theme
      .item
        display: flex
        box-sizing: border-box
        align-items: center
        padding: 0 20px 20px 20px
        .icon
          flex: 0 0 60px
          width: 60px
          padding-right: 20px
        .text
          display: flex
          flex-direction: column
          justify-content: center
          flex: 1
          line-height 20px
          overflow: hidden
          font-size: $font-size-medium
          .name
            margin-bottom: 10px
            color: $color-text
          .desc
            color: $color-text-d
    .loading-container
      position:absolute
      width: 100%
      top: 50%
      transform: translateY(-50%)
</style>